package practice.arraysort;

import java.util.Arrays;
import java.util.Random;

/** 插入排序 */
public class InsertSort {
    public static void main(String[] args) {
        int[] arr = new int[10];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(100);
        }
        System.out.println(Arrays.toString(arr));

        int[] arr1 = {78, 14, 63, 36, 46, 1, 34, 85, 15, 36};
        /*
          插入排序和抓扑克牌很像
         */

        for (int i = 1; i < arr.length; i++) {
            int j = i;
            while (j>0){
                if (arr[j]<arr[j-1]){
                    int tem = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = tem;
                    j--;
                }else{
                    break;
                }
            }
        }
        System.out.println(Arrays.toString(arr));


        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 1+i; j < arr.length; j++) {
                if (arr[i]>arr[j]){
                    int tem = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tem;
                }
            }
        }

        System.out.println(Arrays.toString(arr));

    }
}
